package com.tuotu.gps.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tuotu.gps.R;
import com.tuotu.gps.UploadActivity2;
import com.tuotu.gps.entity.LocationInfo;
import com.tuotu.gps.entity.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;
import java.util.Vector;

/**
 * Created by XuYeWen on 2018/1/30.
 */

public class UploadinglFragment extends BaseFragment implements UploadActivity2.OnQueryResultListener {


    private Vector<LocationInfo> list=new Vector<>();
    private ListView listView;
    private MyArrarAdapter adapter;
    private MyHandler myHandler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_uploading, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        EventBus.getDefault().register(this);
        listView= (ListView) view.findViewById(R.id.uploadingListview);
        myHandler=new MyHandler(this);
        ((UploadActivity2)getActivity()).setOnQueryResultListener(this);
//        myHandler.sendEmptyMessage(0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if(null!=myHandler)
            myHandler.removeCallbacksAndMessages(null);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MainRun(MessageEvent event){
    }

    @Override
    public void onActivityQuery(Vector<LocationInfo> vectors) {
        Log.e("xyww","UploadinglFragment onActivityQuery");
        list=vectors;
        adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
        listView.setAdapter(adapter);

        ((UploadActivity2)getActivity()).setTitles(0,list.size());
    }

    @Override
    public void onActivitySuccess(LocationInfo locationInfo) {
//        Log.e("xyww","UploadinglFragment onActivitySuccess");
        list.remove(locationInfo);
        if(null==adapter){
            adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
            listView.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
        ((UploadActivity2)getActivity()).setTitles(0,list.size());
    }

    @Override
    public void onActivityFail(LocationInfo locationInfo) {
//        Log.e("xyww","UploadinglFragment onActivityFail");
        list.remove(locationInfo);
        if(null==adapter){
            adapter=new MyArrarAdapter(getActivity(),R.layout.layout_list_item,list);
            listView.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
        ((UploadActivity2)getActivity()).setTitles(0,list.size());
    }

    @Override
    public void onResume() {
        super.onResume();
        if(null!=adapter){
            adapter.notifyDataSetChanged();
        }
    }

    //    private void onResult(LocationInfo locationInfo){
//        list.remove(locationInfo);
//        adapter.notifyDataSetChanged();
//        LogUtil.print("uploadingFragment list-----=="+list.size());
//        if(list.size()==0){
//            userPreferences.edit().putBoolean(Preference.User.PREFERENCE_UPLOAD_STATUS,false);
//            EventBus.getDefault().post(new MessageEvent(MessageEvent.MSG_UPLOAD_FILE_DONE));
//        }
//    }

    static class MyArrarAdapter extends ArrayAdapter {
        private Vector<LocationInfo> lists;

        public MyArrarAdapter(Context context, int resource, Vector<LocationInfo> lists) {
            super(context, resource, lists);
            this.lists=lists;
        }

        public void setLists(Vector<LocationInfo> lists) {
            this.lists = lists;
        }

        @Nullable
        @Override
        public String getItem(int position) {
            return lists.get(position).getMp3Name();
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            View view=super.getView(position, convertView, parent);
            TextView tv=view.findViewById(android.R.id.text1);
            String name=getItem(position);
            tv.setText(name.substring(0,name.length()-4));
            return view;
        }
    }

    private static class MyHandler extends Handler {
        private final WeakReference<UploadinglFragment> reference;

        public MyHandler(UploadinglFragment fragment){
            reference=new WeakReference<UploadinglFragment>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            if(null==reference.get()){
                return;
            }
            UploadinglFragment fragment=reference.get();
            switch (msg.what){
                case  0:
                    removeMessages(0);
                        fragment.list=((UploadActivity2)fragment.getActivity()).getAllVectors();
                        if(null!=fragment.list){
                            if(null==fragment.adapter){
                                fragment.adapter=new MyArrarAdapter(fragment.getActivity(),R.layout.layout_list_item,fragment.list);
                                fragment.listView.setAdapter(fragment.adapter);
                            }
                            fragment.adapter.setLists(fragment.list);
                            fragment.adapter.notifyDataSetChanged();
//                        fragment.adapter.notifyDataSetChanged();
                        }
                    sendEmptyMessageDelayed(0,500);
                    break;
                case 1:
                    removeMessages(0);
                    break;
                case 2:
                    break;

            }

        }
    }
}
